package com.note.backend.controller.ai;

import com.note.backend.pojo.vo.MessageVO;
import com.note.backend.repository.ChatHistoryRepository;


import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    private final ChatMemory chatMemory;

    private final ChatHistoryRepository chatHistoryRepository;
    public ChatHistoryController(ChatMemory chatMemory,
                                @Qualifier("inMemoryChatHistoryRepository") ChatHistoryRepository chatHistoryRepository) {
        this.chatMemory = chatMemory;
        this.chatHistoryRepository = chatHistoryRepository;
    }

    @RequestMapping("/{type}")
    public List<String> getChatIds(@PathVariable("type") String type) {
        return chatHistoryRepository.getChatIds(type);
    }

    @RequestMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId) {
        List<Message> messages = chatMemory.get(chatId);
        if (messages == null)
        {
            return List.of();
        }
        return messages.stream().map(MessageVO::new).toList();
    }
}
